import java.util.Objects;

public class TestHeap {
    private int[] elem;  //建立数组
    private int usedSize;
    private void change(int i,int j){
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }
    public void initHeap(int[] array){  //对数组元素进行计数
        for (int i = 0; i < elem.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
    }
    private void shiftDown(int parent,int usedSize){  //下沉式调换
        int child = (2*parent)+1;//左孩子的下标
        while (child < usedSize){
            if (child+1 < usedSize && elem[child] < elem[child+1]){//选出孩子中较大的那个
                child++;
            }
            if (elem[parent] < elem[child]){
                change(parent,child);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }
    public void heapSort(){  //堆排序
        int end = usedSize - 1;
        while (end > 0){
            change(0,end);
            shiftDown(0,end);
            end--;
        }
    }
}
class Student{
    public String name;

    @Override
    public boolean equals(Object obj) {
        Student o  = (Student) obj;
        return o.name.equals(this.name);
    }
}
